package com.jiajunsong.blog.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jiajunsong.blog.domain.Blog;
import com.jiajunsong.blog.domain.Catalog;
import com.jiajunsong.blog.domain.Result;
import com.jiajunsong.blog.domain.User;
import com.jiajunsong.blog.repository.BlogRepository;
import com.jiajunsong.blog.repository.CatalogRepository;
import com.mysql.jdbc.StringUtils;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
public class CatalogController {

	@Autowired
	private CatalogRepository catalogRepository;

	@Autowired
	private BlogRepository blogRepository;

	@GetMapping("/catalogs")
	@ApiOperation(value = "得到当前登录用户的所有分类")
	public Result catalogs(Authentication authentication) {
		if (authentication == null) {
			return new Result("未登录");
		}
		User user = (User) authentication.getPrincipal();
		List<Catalog> catalogs = catalogRepository.findByUser(user);
		return new Result("", catalogs);
	}

	@PostMapping("/catalog")
	public Result addCatalog(Authentication authentication, @RequestBody Catalog catalog) {
		if (authentication == null) {
			return new Result("未登录");
		}

		if (StringUtils.isNullOrEmpty(catalog.getName())) {
			return new Result("分类的名称不能为空");
		}

		User user = (User) authentication.getPrincipal();

		if (catalog.getId() != null) {
			Optional<Catalog> optionalCatalog = catalogRepository.findById(catalog.getId());
			if (optionalCatalog.isPresent()) {
				Catalog catalog_q = optionalCatalog.get();
				if (catalog_q.getUser().getId().longValue() == user.getId().longValue()) {
					catalog.setUser(user);
					catalogRepository.save(catalog);
					return new Result("", catalog);
				} else {
					return new Result("所要更改的catalog不属于当前登录用户，不能更改");
				}
			}
		}
		catalog.setUser(user);
		catalogRepository.save(catalog);
		return new Result("", catalog);

	}

	@DeleteMapping("/catalog")
	@ApiOperation(value = "根据id删除对应的Catalog，并且把此Catalog对应的Blog的对应的Catalog设置为空")
	public Result deleteCatalog(@ApiParam(value = "catalog的id") @RequestParam(required = true) Long id,
			Authentication authentication) {
		if (authentication == null) {
			return new Result("未登录");
		}
		User user = (User) authentication.getPrincipal();
		Optional<Catalog> optionalCatalog = catalogRepository.findById(id);
		if (optionalCatalog.isPresent()) {
			Catalog catalog = optionalCatalog.get();
			User catalogUser = catalog.getUser();
			if (catalogUser.getId().longValue() != user.getId().longValue()) {
				return new Result("你只能删除自己的Catalog,无权删除别人名下的Catalog");
			} else {
				List<Blog> blogs = blogRepository.findByCatalog(catalog);
				for (Blog blog : blogs) {
					blog.setCatalog(null);
					blogRepository.save(blog);
				}
				catalogRepository.deleteById(id);
				return new Result("");
			}
		} else {
			return new Result("根据指定的id没有找到Catalog");
		}
	}
}
